﻿using Newtonsoft.Json;

using PLCS.App.Shared.Extensions;
using PLCS.App.Shared.Models;
using PLCS.Client.IService.LogManager;

namespace PLCS.App.Shared.ViewModels;

public class BaseViewModel : BindableBase
{
    public readonly IDialogService dialog;
    public readonly IOperationLogService _operationLogService;
    public string View { get; set; }

    public BaseViewModel()
    {
        dialog = ContainerLocator.Container.Resolve<IDialogService>();
        _operationLogService = ContainerLocator.Container.Resolve<IOperationLogService>();
    }

    public void Log(string operationType, object? operationObject = null, string? description = null)
    {
        _operationLogService.CreateAsync(
            new Application.Contracts.LogShowApp.Dtos.OperationLogEditDto()
            {
                User = GlobalVariable.CurrentUser.UserName,
                Description = description,
                OperationObject = JsonConvert.SerializeObject(operationObject),
                OperationType = operationType,
                View = View,
            }
        );
    }

    public void ShowMessage(string msg)
    {
        dialog.ShowMessage("消息提示", msg);
    }

    public bool ShowQuestion(string msg)
    {
        return dialog.Question("确认信息", msg);
    }
}